package utils

import (
	"fmt"
	"go.uber.org/zap"
	"os/exec"
	"runtime"
	"shorturl/log"
)

var commands = map[string]string{
	"windows": "cmd",
	"darwin":  "open",
	"linux":   "xdg-open",
}

func Openurl(uri string) error {
	run, ok := commands[runtime.GOOS]
	if !ok {
		log.Logger.Error("don't know how to open things on %s platform", zap.String("platform", runtime.GOOS))
		return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
	}
	if runtime.GOOS == "windows" {
		return exec.Command(run, "/c", "start", uri).Start()
	} else {
		return exec.Command(run, uri).Start()
	}
}
